<?php 
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
/**
*
* @version $Id$
* @package VirtueMart
* @subpackage html
* @copyright Copyright (C) 2010 Forrest Chamberlain. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
* http://virtuemart.net
*/

/**
 * In this page, we display:
 *  - the current user's wishlists, or 
 *  - the contents of a user's wishlist if wishlist_id is present (and the user is logged-in), or
 *  - the list of public wishlists for which a third party searched, or
 *  - the contents of a public wishlist to a third party
 */
mm_showMyFileName( __FILE__ );

require_once(CLASSPATH . 'ps_wishlist.php' );
require_once(CLASSPATH . 'ps_product.php' );
require_once(CLASSPATH . 'template.class.php' );

$sent_user_id = intval( JRequest::getInt('uid'));
$wishlist_id = intval( JRequest::getInt('wishlist_id'));
$category_id = intval( JRequest::getInt('category_id'));
$product_id = intval( JRequest::getInt('product_id'));
$wishlist_code = isset($_GET['wishlist_code']) ? $_GET['wishlist_code'] : NULL;

$output = '';
$result = false;

// was a user specified?
if (isset( $sent_user_id ) && $sent_user_id > 0 && $sent_user_id != $GLOBALS['my']->id)
{
	$user = ps_wishlist::get_user_info( $sent_user_id );

	// show wishlists for the specified user (only public wishlists will show)
	// show_wishlists will return false if there's no public wishlists
	if ($user !== false)
	{
		$result = show_wishlists( $output, $user );
	}

}

// no user specified -- wishlist code?
if ($result === false && isset( $wishlist_code ) && !empty( $wishlist_code ) && trim($wishlist_code) != '')
{
	// convert wishlist_code to wishlist_id
	$return_value = ps_wishlist::wishlist_code_to_id( $wishlist_code );
	if ($return_value !== false)
	{
		// this will take precedence over any wishlist id that might have been passed
		$wishlist_id = $return_value;
	}
}

// wishlist id?
if ($result === false && isset( $wishlist_id ) && !empty( $wishlist_id ) && $wishlist_id > 0)
{
	// will only show the wishlist if the current user is the owner, or it is public
	// if neither, it returns false
	$result = show_wishlist_items( $output, $wishlist_id, $category_id );
}

// nothing yet, if we're logged-in, show our own wishlists
if ($result === false && (isset( $GLOBALS['my'] ) && $GLOBALS['my']->id > 0))
{
	$result = show_wishlists( $output, $GLOBALS['my'] );
}

// still nothing, show the login form
if ($result === false)
{
		// display login form
		show_login( $output, $wishlist_id, $wishlist_code, $sent_user_id );
}

echo $output;


/* ----------- */


/**
 * Show a list of wishlists
 * @author Forrest
 * @param string $output -- the output of this function will be added to this parameter
 * @param object $user
 * @return boolean -- successful or not
 */
function show_wishlists( &$output, &$user )
{

	$page_title = $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_LIST_TITLE') . $user->name;
	$GLOBALS['mainframe']->appendPathWay( $page_title );
	
	$wl_list = ps_wishlist::get_wishlists( $user->id );

	if ($wl_list !== false)
	{
		// which template do we use?
		$tpl = ( ($user->id == $GLOBALS['my']->id) ? 'private' : 'public' );

		$wishlists = '';
		$base_url = URL . 'index.php?option=' . $GLOBALS['option'] . '&page=wishlist.index&Itemid=' . $GLOBALS['sess']->getShopItemid();

		// the "row" template -- for each of the wishlists in this page
		$template = read_file( VM_THEMEPATH."templates/wishlist/$tpl/wishlist_list_row.php" );

		foreach ($wl_list as $wl)
		{
			// prep
			$url = JRoute::_($base_url . '&wishlist_id=' . $wl['wishlist_id'] . '&uid=' . $user->id);

			// replace vars in template
			$tmp = str_replace( "{link}", $url, $template );
			$tmp = str_replace( "{name}", $wl['name'] . ' (' . $wl['item_count'] . ')' , $tmp );
			$tmp = str_replace( "{comments}", $wl['comments'], $tmp );
			$tmp = str_replace( "{created}",  date( 'M d, Y', strtotime( $wl['created'] ) ), $tmp );
			$tmp = str_replace( "{modified}", date( 'M d, Y', strtotime( $wl['modified'] ) ), $tmp );
			$tmp = str_replace( "{public}", $wl['public'], $tmp );

			$wishlists .= $tmp;
		}

		// prep
		$msg = '';
		if (count( $wl_list ) == 0) 
		{
			// say that there's no wishlists
			$msg .= $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_NO_WISHLISTS');
		}
		$wishlist_form_action =  JRoute::_($base_url . '&uid=' . $user->id);
		$wishlist_form_name = 'addwishlist';
		$wishlist_form_hidden = '<input type="hidden" name="page" value="wishlist.index" />';
		$wishlist_form_hidden .= '<input type="hidden" name="func" value="wishlistAdd" />';
		$wishlist_form_hidden .=  '<input type="hidden" name="option" value="' . $GLOBALS['option'] . '" />';
		$wishlist_form_hidden .=  '<input type="hidden" name="Itemid" value="' . $GLOBALS['sess']->getShopItemid() . '" />';


		// the overall page template -- for the listing of wishlists (public or private listing)
		$to_output = read_file( VM_THEMEPATH."templates/wishlist/$tpl/wishlist_list.php" );
		$to_output = str_replace( "{page_title}", $page_title, $to_output );
		$to_output = str_replace( "{wishlist_list}", $wishlists, $to_output );
		$to_output = str_replace( "{wishlist_form_title}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_ADD_NEW'), $to_output );
		$to_output = str_replace( "{wishlist_form_action}", $wishlist_form_action, $to_output );
		$to_output = str_replace( "{wishlist_form_name}", $wishlist_form_name, $to_output );
		$to_output = str_replace( "{wishlist_form_hidden}", $wishlist_form_hidden, $to_output );
		$to_output = str_replace( "{wishlist_name_name}", 'wishlist_name', $to_output );
		$to_output = str_replace( "{wishlist_name}", '', $to_output );
		$to_output = str_replace( "{wishlist_submit}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_ADD'), $to_output );
		$to_output = str_replace( "{wishlist_message}", $msg, $to_output );

		// copy to overall output
		$output .= $to_output;

		return true;
	}

	return false;
}

/**
 * Show a wishlist -- and its items
 * @author Forrest
 * @param string $output -- the output of this function will be added to this parameter
 * @param int $wishlist_id
 * @return boolean -- successful or not
 */
function show_wishlist_items( &$output, $wishlist_id, $category_id = "")
{
	// get the wishlist details
	$wl = ps_wishlist::get_wishlist( $wishlist_id );

	if ($wl !== false)
	{
		// which template do we use?
		if ($wl['user_id'] == $GLOBALS['my']->id)
		{
			$user = $GLOBALS['my'];
			$tpl = 'private';
		}
		else
		{
			$user = ps_wishlist::get_user_info( $wl['user_id'] );
			$tpl = 'public';
		}

		// get this wishlist's items
		$wl_items = ps_wishlist::get_wishlist_items( $wishlist_id );

		$wishlist_item_list = '';
		if (count( $wl_items ) > 0)
		{
			// the "row" template -- for each of the wishlist's items
			$template = read_file( VM_THEMEPATH . "templates/wishlist/$tpl/wishlist_item_row.php" );

			foreach ($wl_items as $wl_item) 
			{
				// prep
				$base_url = URL . 'index.php?option=com_virtuemart&wishlist_id='.$wishlist_id.'&wishlist_item_id='.$wl_item['wishlist_item_id'].
				'&Itemid='.$GLOBALS['sess']->getShopItemid();

				$remove_link = $base_url . '&page=wishlist.index&func=wishlistDeleteItem';
				$action_url = $base_url . '&page=wishlist.index&func=wishlistUpdateItem';

				$addtocart_title = $addtocart_link = '';
				$tmpl = vmTemplate::getInstance();
				if (USE_AS_CATALOGUE != '1')
				{
					$addtocart_title = $GLOBALS['VM_LANG']->_('PHPSHOP_CART_ADD_TO');
					$addtocart_link = $base_url . '&page=shop.cart&func=wishlistAddItemtocart';
					if( $tmpl->get_cfg( 'useAjaxCartActions', 1 ) ) {
						$addtocart_link .= '&func=cartAdd';
					} else {
						$addtocart_link .= '&func=wishlistAddItemtocart';
					}
				}


				// replace vars in template
				$tmp = str_replace( "{action_url}", $action_url, $template );
				$tmp = str_replace( "{product_url}", $wl_item['product_link'], $tmp );
				$tmp = str_replace( "{product_image}", $wl_item['product_thumb_image'], $tmp );
				$tmp = str_replace( "{product_name}", $wl_item['product_name'], $tmp );
				$tmp = str_replace( "{product_price}", $wl_item['product_price'], $tmp );
				$tmp = str_replace( "{product_quantity}", $wl_item['product_quantity'], $tmp );
				$tmp = str_replace( "{product_s_desc}", $wl_item['product_s_desc'], $tmp );
				$tmp = str_replace( "{product_desc}", $wl_item['product_desc'], $tmp );
				$tmp = str_replace( "{description}", $wl_item['description'], $tmp );
				$tmp = str_replace( "{prodid}", $wl_item['product_id'], $tmp );

				$tmp = str_replace( "{quantity_name}", 'quantity', $tmp );
				$tmp = str_replace( "{uniqid}", uniqid('addtocart_'), $tmp );
				$tmp = str_replace( "{addtocart_link}", $addtocart_link, $tmp );
				$tmp = str_replace( "{remove_link}", $remove_link, $tmp );
				$tmp = str_replace( "{addtocart_title}", $addtocart_title, $tmp );
				$tmp = str_replace( "{remove_title}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_REMOVE_FROM'), $tmp );

				if( $tmpl->get_cfg( 'useAjaxCartActions', 1 ) ) { 
					$onclick = 'onsubmit="handleAddToCart( this.id );return false;"';
					$tmp = str_replace( "{onclick}", $onclick, $tmp );
				} else {
					$onclick = '';
					$tmp = str_replace( "{onclick}", $onclick, $tmp );
				}
				$attributes = '';
				if ($wl_item['has_attributes']) {
					$descript = explode(";", $wl_item['has_attributes']);
					foreach ($descript as $inner_descript) {
						$final_descript = explode(":", $inner_descript);
						$attributes .= '<input type="hidden" name="'.$final_descript[0].$wl_item['product_id'].'" value="'.$final_descript[1].'" />';
					}
				}
				$tmp = str_replace( "{attributes}", $attributes, $tmp );

				$wishlist_item_list .= $tmp;
			}
		}
		else
		{
			$wishlist_item_list = $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_NO_WISHLISTS_ITEMS');
		}

		// the overall page template for this wishlist
		$to_output = read_file( VM_THEMEPATH . "templates/wishlist/$tpl/wishlist_item_list.php" );

		// prep
		$page_title = $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_TITLE') . ': ' . shopMakeHtmlSafe($wl['name']);
		if ($GLOBALS['my']->id > 0) {
		$created_by = $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_CREATED_BY') . ' ' . $user->name;
		} else {
		$created_by = $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_CREATED_BY') . ' ' . $user;
		}

		$base_url = URL . 'index.php?option=com_virtuemart&wishlist_id=' . $wishlist_id;

		$remove_link = $base_url . '&page=wishlist.index&func=wishlistDelete';
		$save_action = $base_url . '&page=wishlist.index&func=wishlistUpdate';

		$addtocart_link = $addtocart_title = '';
		if (USE_AS_CATALOGUE != '1')
		{
			$addtocart_link = $base_url . '&page=shop.cart&func=wishlistAddtocart';
			$addtocart_title = $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_CART_ADD_TO');
		}

		if ($wl['public'] == 'N')
		{
			$share_title = $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_SHARE');
			$share_link = $base_url . '&page=wishlist.share&func=wishlistShare';
		}
		else
		{
			$share_title = $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_STOP_SHARE');
			$share_link = $base_url . '&page=wishlist.index&func=wishlistStopShare';
		}

		if (($category_id) || !empty($product_id))
		{
			$continue_title = $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_CONTINUE');
			if ($category_id != "") {
				$continue_link = '<a href="'.$base_url.'&page=shop.browse&category_id='.$category_id.'&Itemid='.$GLOBALS['sess']->getShopItemid().
				'" title="'.$continue_title.'" class="wishlist-continue">'.$continue_title.'</a>';
			} else {
				$continue_link = '<a href="'.$base_url.'&page=shop.product_details&product_id='.$product_id.'&Itemid='.
				$GLOBALS['sess']->getShopItemid().'" title="'.$continue_title.'" class="wishlist-continue">'.$continue_title.'</a>';
			}
		}
		else
		{
			$continue_link = '';
		}

		// Print Button - used to pop-up print window & print page
		$isPopup = vmGet( $_GET, 'pop' );
		if ( $isPopup ) {
			$has_print = '<span class="vmNoPrint">';
			$has_print .= '<a href="javascript:void()" onclick="javascript:window.print(); return false;" title="Print" style="margin-right:10px;">';
			$has_print .= 'Print</a></span>';
		} else { 
			$wishlist_code = isset($_GET['wishlist_code']) ? $_GET['wishlist_code'] : '';
			$wishlist_id = isset($_GET['wishlist_id']) ? $_GET['wishlist_id'] : '';
				if ($wishlist_code) {
					$wl_print_url = "wishlist_code=".$wishlist_code;
				}
				else if ($wishlist_id) {
					$wl_print_url = "wishlist_id=".$wishlist_id;
				}
				else
				{
					$wl_print_url = '';	
				}
			$has_print = '<span class="vmNoPrint">';
			$has_print .= '<a href="javascript:void()" onclick="javascript:window.open(\' '. URL .'/index2.php?option=com_virtuemart&amp;page=wishlist.index&amp;'. $wl_print_url .'&amp;pop=1\', \'width=640\', \'height=480\')" title="Print"  style="margin-right:10px;">';
			$has_print .= 'Print</a></span>';
		}

		// replace vars in template
		$to_output = str_replace( "{created_by}", $created_by, $to_output );
		$to_output = str_replace( "{page_title}", $page_title, $to_output );
		$to_output = str_replace( "{name}", $wl['name'], $to_output );
		$to_output = str_replace( "{name_name}", 'wishlist_name', $to_output );
		$to_output = str_replace( "{comments}", $wl['comments'], $to_output );
		$to_output = str_replace( "{comments_name}", 'wishlist_comments', $to_output );
		$to_output = str_replace( "{created}", date( 'M d, Y', strtotime( $wl['created'] ) ), $to_output );
		$to_output = str_replace( "{modified}", date( 'M d, Y', strtotime( $wl['modified'] ) ), $to_output );
		$to_output = str_replace( "{public}", $wl['public'], $to_output );
		$to_output = str_replace( "{wishlist_item_list}", $wishlist_item_list, $to_output );
		$to_output = str_replace( "{save_action}", $save_action, $to_output );
		$to_output = str_replace( "{submit_name}", 'submit', $to_output );
		$to_output = str_replace( "{submit}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_SAVE'), $to_output );
		$to_output = str_replace( "{addtocart_link}", $addtocart_link, $to_output );
		$to_output = str_replace( "{addtocart_title}", $addtocart_title, $to_output );
		$to_output = str_replace( "{remove_link}", $remove_link, $to_output );
		$to_output = str_replace( "{remove_title}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_REMOVE'), $to_output );
		$to_output = str_replace( "{remove_confirm_text}", $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_REMOVE_CONFIRM'), $to_output );
		$to_output = str_replace( "{share_link}", $share_link, $to_output );
		$to_output = str_replace( "{share_title}", $share_title, $to_output );
		$to_output = str_replace( "{continue_link}", $continue_link, $to_output );
		$to_output = str_replace( "{wl_print}", $has_print, $to_output );

		// copy to overall output
		$output .= $to_output;

		return true;
	}

	return false;
}


function show_login( &$output, $wishlist_id, $user_id ) 
{
	global $VM_LANG, $mm_action_url, $mosConfig_allowUserRegistration;

	$to_output = '<div class="wishlist-login" style="border:1px solid #999999;margin-top:15px;padding:6px;">';
	$to_output .= '<h4>Please Login to Create Wishlist</h4>';

	$_SERVER['QUERY_STRING'] .= '&option=' . $GLOBALS['option'];
	if (isset( $wishlist_code ) && !empty( $wishlist_code ))
	{
		$_SERVER['QUERY_STRING'] .= '&wishlist_code=' . $wishlist_code;
	}
	else
	{
		$_SERVER['QUERY_STRING'] .= '&wishlist_id=' . $wishlist_id;
	}
	$_SERVER['QUERY_STRING'] .= '&uid=' . $user_id;
	$_SERVER['QUERY_STRING'] .= '&Itemid=' . $GLOBALS['sess']->getShopItemid();
	$_SERVER['QUERY_STRING'] .= '&page=wishlist.index';

	ob_start();
	include( PAGEPATH . 'checkout.login_form.php' );
	$to_output .= ob_get_contents();
	ob_end_clean();

	$to_output .= "or <a href=\"".JRoute::_('index.php?option=com_virtuemart&amp;page=shop.registration', true, 0)."\" title=\"Register\">Create an Account to Get Started</a>";
	$to_output .= '</div>';

	$output .= $to_output;

	return true;
}
?>
